/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2012 All Rights Reserved.
 */
package com.xsq.util;

import java.security.GeneralSecurityException;

import javax.crypto.Cipher;

import org.codehaus.xfire.util.Base64;

/**
 * 
 * 加验签工具类
 * 
 * @author geng.lin
 * @version $Id: SignUtilsImpl.java, v 0.1 2012-2-24 下午02:34:17 geng.lin Exp $
 */
public class SignUtilsImpl implements SignUtils {

	/**
	 * 加签
	 * 
	 * @param text
	 *            明文
	 * @param key
	 *            密钥
	 * @param algorithm
	 *            签名算法,目前KMI支持NONEwithRSA, MD2withRSA, MD5withRSA, SHA1withRSA,
	 *            SHA256withRSA, SHA384withRSA, SHA512withRSA , SHA1withDSA
	 * @return 签名
	 * @throws Exception
	 */
	public String sign(final String text, final String key, final String algorithm) {
		final byte[] textBytes = text.getBytes();
		final byte[] keyBytes = Base64.decode(key);
		byte[] resultBytes = null;
		try {
			resultBytes = SignatureUtil.sign(textBytes, keyBytes, algorithm);
			return Base64.encode(resultBytes);
		} catch (GeneralSecurityException e) {
			e.printStackTrace();
			return "";
		}

	}

	/**
	 * 验签
	 * 
	 * @param text
	 *            明文
	 * @param signText
	 *            签名
	 * @param key
	 *            密钥
	 * @param algorithm
	 *            验签算法,目前KMI支持NONEwithRSA, MD2withRSA, MD5withRSA, SHA1withRSA,
	 *            SHA256withRSA, SHA384withRSA, SHA512withRSA , SHA1withDSA
	 * @return 验签通过返回true，不通过返回false
	 * @throws Exception
	 */
	public boolean verify(final String text, final String signText, final String key, final String algorithm) {

		try {
			return SignatureUtil.verify(text.getBytes(), Base64.decode(signText), Base64.decode(key), algorithm);
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 加密
	 * @see com.test.common.crypto.alipay.virtualprod.common.crypto.SignUtils#encrypt(String,
	 *      String, String)
	 */
	public String encrypt(String text, String key, String algorithm) {

		

		try {
			
			byte[] bytes = text.getBytes("UTF-8"); // 待加/解密的数据
			byte[] keyData = Base64.decode(key); // 密钥数据
			byte[] cipherBytes = SymmtricCryptoUtil.symmtricCrypto(bytes, keyData, algorithm, Cipher.ENCRYPT_MODE);
			return Base64.encode(cipherBytes);
		} catch (Exception e) {
			return "";
		}
	}


	
	public static void main(String[] args) {
		/** 私钥 */
		
		  String PRI_KEY2 = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIFfinYjuBgtgWQs/eXleUKoW/vN/1dYWbBCRCbPTwZRO/E5Xt3Z1gd/LsorZL/Q88/cTF9D9/qpyK9gO5TjC0rIiicZm2nskiH4Po0gWD8iyR7nsfth5yp6Y3b4L+fazjPzyJvP74yYHDwMnyke6iUub8UTDvRwXECrrZ0GnvPpAgMBAAECgYBGJ9w68mW7Xrni6+8HQyg9uHShGPVVlUiVp2mzrgnJgDXEOvsxzRYeXJ6v7VYmGpaMaodTxdTsrjNwvvuwYmrVZXSDDy9lrkXADdm+BkclfU5gB4cok8tL0YIjX7gmojVTUjxSV3aYupSLuAtFanoCti7Mk7uBYU77ySvUibK4gQJBAPYsQeFgy74s6taSmxJ8eVzfvSDOTR2hV+R3tNRAkvedC53Vk6ahH31Sjj+QbXvhSq5dmoYEUAnkTS5uqMoQKFkCQQCGiajFcouzJmvewtw8KukHvnvJ9r9Ltvft4aN0ggD1ZwNivOT/AjpMUOtzRI9wWx1Ng57q1CIzc6hBnLNaJbYRAkBQ3zU9b2k/rs3vc/ZR2macZjxhvKSE6W5QdisH17bwdb1NOs7o3OHVb/ufUjlFizvXkJSlFmGc/k2ExYHZ7/jxAkAYGTJU47aoSVqLFUZTtK/3BiSwQXV0f3qbbo5TwTGgw2n01rcCAokkwF3cUMyMqJVwHAJsN8FEjBP4ZLQvko+xAkEAgCicDqtH8lY5bwXAaQnXXPsQGuMmlGEJPmhKFUsofPVSKjEdoyC2FDEh0WvQwm2FUcIhTgLmDnflrBpOdIWIAQ==";
		/** 公钥 */
		
		  String PUB_KEY2 = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCBX4p2I7gYLYFkLP3l5XlCqFv7zf9XWFmwQkQmz08GUTvxOV7d2dYHfy7KK2S/0PPP3ExfQ/f6qcivYDuU4wtKyIonGZtp7JIh+D6NIFg/Iske57H7YecqemN2+C/n2s4z88ibz++MmBw8DJ8pHuolLm/FEw70cFxAq62dBp7z6QIDAQAB";
		
		  
		  String text = "<info><a>aaaaaaaaa</a>bbbbbbbbbbb<b></b></info>";
		  SignUtils tt = new SignUtilsImpl();
		  
		  
		  
		 // String s1 = tt.sign(text, PRI_KEY2, "SHA1withRSA");
		  
		//  System.out.println(tt.verify(text, s1, PUB_KEY2, "SHA1withRSA"));
		  
		  
		  
		//SignUtils tt = new SignUtilsImpl();
		String ss = tt.encrypt(text, "CilYtvADaIXMBSo/KfhV6hOdYnIUJond", "DESede");
		
		//
		System.out.println(ss);
		String s2 = tt.decrypt(ss, "CilYtvADaIXMBSo/KfhV6hOdYnIUJond", "DESede");
		System.out.println(s2);
	}
	
	/**
	 * 解密
	 * @see com.test.common.crypto.alipay.virtualprod.common.crypto.SignUtils#decrypt(String,
	 *      String, String)
	 */
	public String decrypt(String text, String key, String algorithm) {

		byte[] bytes = Base64.decode(text); // 待加/解密的数据
		byte[] keyData = Base64.decode(key); // 密钥数据

		try {
			byte[] cipherBytes = SymmtricCryptoUtil.symmtricCrypto(bytes, keyData, algorithm, Cipher.DECRYPT_MODE);
			return new String(cipherBytes,"UTF-8");
		} catch (Exception e) {
			return "";
		}
	}

}
